home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Personal Computer World 2009 February
/
PCWFEB09.iso
/
Software
/
Freeware
/
Read It Later 0.9924
/
read_it_later-0.9924-fx.xpi
/
chrome
/
isreaditlater.jar
/
content
/
ISRILoffline.js
< prev
next >
Wrap
Text File
|
2008-10-28
|
13KB
|
375 lines
function ISRILoffline()
{
this.numOfThreads = 2;
this.viewing = null;
this.queue = new Object();
this.queueItemLoaded = new Object();
this.queueSize = 0;
this.threadsOpen = new Array();
this.threadTO = 14 * 1000;
this.threadTOs = new Object();
}
ISRILoffline.prototype = {
_init : function() {
//File delim
this.FD = ((navigator.platform=='Win32')?('\\'):('//'));
Components.utils.import("resource://rilglobals/ISRILglobals.js");
//File Service
this.sFile = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties);
this.file = this.sFile.get("ProfD", Components.interfaces.nsIFile);
//Download Prefs
this.bPrefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("browser.");
//Directory Locations
this.prefix = 'RIL ';
this.offlineFolderName = 'Read-It-Later Offline';
this.offlinePath = this.file.path + this.FD + this.offlineFolderName;
//Create Offline Folder If it Doesn't Exist
this.file.append( this.offlineFolderName );
if( !this.file.exists() || !this.file.isDirectory() ) { // if it doesn't exist, create
this.file.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0777);
ISRILprefs.setPref("offline_dir", this.offlinePath);
}
/*if (ISRILxul.bip('iframes') && ISRILxul.bip('iframes').tagName == 'panel') {
ISRILxul.bip('iframes').setAttribute('hidden', false);
ISRILxul.bip('iframes').openPopup( ISRILxul.bip('content') , "after_end", -5000, -5000);
}*/
if (ISRILglobals.flagRenameFiles) { this.renameFiles(); }
},
GoOffline : function(single) {
if (navigator.onLine) {
//This part here seems wasteful, but we can't modify the result list directly (cannot modify properties of a wrappednative)
ISRILoffline.queueSize = 0;
var resultList = ISRIL.GetList(11);
for(i in resultList) {
ISRILoffline.queue[ ISRILoffline.queueSize ] = {itemId:resultList[i].itemId, uri:resultList[i].uri};
ISRILoffline.queueSize++;
}
ISRILoffline.OfflineStep(2);
ISRILoffline.startQueue();
} else {
ISRILxul.sPrompt.alert(window, ISRIL.l('OfflineTitle'), ISRIL.l('OfflineOffline'));
}
},
addToQueue : function(id,resolve) {
ISRILoffline.queue[ ISRILoffline.queueSize ] = {itemId:id, uri:ISRIL.sBookmarks.getBookmarkURI( id ).spec, resolve:resolve?resolve:false};
ISRILoffline.queueSize++;
ISRILoffline.startQueue();
},
startQueue : function() {
if (navigator.onLine) {
if (!ISRILoffline.queueRun) {
//Restart Queue
ISRILoffline.queueRun = true;
ISRILoffline.k = true;
ISRILoffline.processed = 0;
ISRILoffline.skipped = 0;
ISRILoffline.timedOut = 0;
ISRILoffline.createIframes();
//when downloading images, make sure it doesn't try to autofit it into the 1x1 hidden iframe
//we store their current setting so we can set it back after the downloads are complete
ISRILoffline.oldbPref = ISRILoffline.bPrefs.getBoolPref('enable_automatic_image_resizing');
ISRILoffline.bPrefs.setBoolPref('enable_automatic_image_resizing', false);
ISRILoffline.tmp = ISRILoffline.bPrefs.getBoolPref('enable_automatic_image_resizing');
//Start - feed first batch into threads
for(t=1; t<=ISRILoffline.numOfThreads; t++) {
ISRILoffline.next();
}
} else {
ISRILoffline.next();
}
}
},
CheckQueue : function(noTO) {
//This could be improved, only having to check from processed items onwards. Since it
//repeats checking pages that obviously have been processed already
var unprocessed = false;
ISRILoffline.counts = {'unprocessed':0,'processing':0,'downloaded':0,'timedOut':0,'skipped':0};
ISRIL.d('----');
for (var qp in ISRILoffline.queue) {
if (!unprocessed && !ISRILoffline.queue[qp].status) {
unprocessed = qp;
}
status = ISRILoffline.queue[qp].status ? ISRILoffline.queue[qp].status : 'unprocessed';
ISRILoffline.counts[ status ]++;
ISRIL.d(qp + ' - ' + status + ' - ' + ISRILoffline.queue[qp].thread );
}
if (ISRILxul.bip('OfflineMeter')) {
//Show Progress
totalDone = ISRILoffline.counts['downloaded'] + ISRILoffline.counts['skipped'] + ISRILoffline.counts['timedOut'];
totalProcessing = totalDone + ISRILoffline.counts['processing'];
ISRILxul.bip('OfflineMeter').value = Math.round( totalProcessing / ISRILoffline.queueSize * 100 );
ISRILxul.bip('OfflineMeterDone').value = Math.round( totalDone / ISRILoffline.queueSize * 100 );
ISRILxul.bip('OfflineMeterText').value = totalDone + ' / ' + ISRILoffline.queueSize;
}
if (!unprocessed && !noTO) { //nothing left to do, wait for timeouts and then force finish
ISRIL.d('final timeout');
//If there is nothing left to process, then only wait 1 second (give time for other items in batch to show up)
waitTime = (ISRILoffline.counts[ 'processing' ] > 0) ? ISRILoffline.threadTO : 1000;
ISRILoffline.finalQueueTO = setTimeout('ISRILoffline.OfflineBatchDone();', waitTime);
}
return unprocessed;
},
next : function() {
/*
Check Queue - If next() was called, something changed, so update the queue meter with check queue and get an unprocessed item if one exists
Go through queue and find the first unprocessed item
Get a thread
Start Processing
No Thread?
Do nothing - wait till a thread is free
*/
var qp = ISRILoffline.CheckQueue();
if (qp) {
if (ISRILoffline.k) {
var freeThread = ISRILoffline.getFreeThread(); // Get a Thread
if (freeThread) { //Has an open Thread
Item = ISRILoffline.queue[qp];
if (!ISRILoffline.getF(Item.itemId).exists()) {
iframe = ISRILxul.bi(ISRILoffline.getIframeId(freeThread));
iframe.setAttribute('i', Item.itemId);
iframe.setAttribute('qp', qp);
iframe.webNavigation.loadURI(Item.uri,Components.interfaces.nsIWebNavigation.LOAD_FLAGS_BYPASS_CACHE,null,null,null);
ISRILoffline.queue[qp].status = 'processing';
ISRILoffline.queue[qp].thread = freeThread;
ISRILoffline.threadTOs[freeThread] = setTimeout('ISRILoffline.threadTimedOut('+freeThread+','+qp+')', ISRILoffline.threadTO);
} else {
ISRILoffline.queue[qp].status = 'skipped';
ISRILoffline.unlockThread(freeThread);
ISRILoffline.next();
}
} //do nothing - wait until a thread is freed
}
}
},
getFreeThread : function() {
for(var i=1; i<=ISRILoffline.threadsOpen.length; i++) {
if (ISRILoffline.threadsOpen[i]) {
ISRILoffline.threadsOpen[i] = false;
return i;
}
}
return false;
},
unlockThread : function(t) {
clearInterval( ISRILoffline.threadTOs[t] );
ISRILoffline.threadsOpen[t] = true;
},
threadTimedOut : function(t, qp) {
ISRILoffline.unlockThread(t);
ISRILoffline.queue[qp].status = 'timedOut';
ISRILoffline.next();
},
createIframes : function() {
if ( !ISRILxul.bi( ISRILoffline.getIframeId(ISRILoffline.numOfThreads-1) ) ) {
for(i=1; i<=ISRILoffline.numOfThreads; i++) {
//http://developer.mozilla.org/en/docs/Code_snippets:HTML_to_DOM
id = ISRILoffline.getIframeId(i);
iframe = ISRILxul.createNode('iframe', {type:"content", thread:i, name:id, id:id, hidden:"false", width:"1000", height:"1000"});
ISRILxul.bip('iframes').appendChild(iframe);
iframe.webNavigation.allowAuth = true;
iframe.webNavigation.allowImages = true;
iframe.webNavigation.allowJavascript = false;
iframe.webNavigation.allowMetaRedirects = true;
iframe.webNavigation.allowPlugins = false;
iframe.webNavigation.allowSubframes = false;
iframe.addEventListener("load", ISRILoffline.iframe_onPageLoad, true);
ISRILoffline.threadsOpen[ i ] = true;
}
}
return true;
},
iframe_onPageLoad: function(aEvent) {
var doc = aEvent.originalTarget;
var url = doc.location.href;
var id = this.getAttribute('i');
var qp = this.getAttribute('qp');
var t = this.getAttribute('thread');
if (ISRILoffline.queue[qp] && !ISRILoffline.queueItemLoaded[qp] && id) {
if (aEvent.originalTarget.nodeName == "#document") { // ok, it's a real page, let's do our magic
ISRILoffline.saveDoc(doc, id);
ISRILoffline.queue[qp].status = 'downloaded';
ISRILoffline.queueItemLoaded[qp] = true;
ISRILoffline.unlockThread(t);
ISRILoffline.next();
}
}
},
saveDoc : function(aDocument, i) {
var wbp = Components.classes['@mozilla.org/embedding/browser/nsWebBrowserPersist;1']
.createInstance(Components.interfaces.nsIWebBrowserPersist);
wbp.saveDocument(aDocument, ISRILoffline.getF( i ), ISRILoffline.openF( ISRILoffline.iURL(i, '_files', true) ), null, null, null);
},
OfflineStep : function(s) {
ISRILoffline.step = s;
if (ISRILxul.bip('OfflineMeter')) {
ISRILxul.bip('OfflineStep1').setAttribute('hidden', ( (s==1) ? (false) : (true) ) );
ISRILxul.bip('OfflineStep2').setAttribute('hidden', ( (s==2) ? (false) : (true) ) );
ISRILxul.bip('OfflineStep3').setAttribute('hidden', ( (s==3) ? (false) : (true) ) );
return true;
}
},
OfflineBatchDone : function() {
if (!ISRILoffline.CheckQueue(true) && ISRILoffline.queueRun) {
ISRIL.d('OFFLINE BATCH DONE');
ISRILoffline.queueRun = false;
if ( ISRILoffline.OfflineStep(3) ) {
var contentStr = ISRIL.l('Alright1') + ' ' + ISRILoffline.counts['downloaded'] + ' ';
contentStr += ISRIL.l('Alright2') + ' ' + ISRILoffline.counts['skipped'] + ' ' + ISRIL.l('Alright3') + ' ';
contentStr += (ISRILoffline.counts['timedOut'] + ISRILoffline.counts['processing']) + ' ' + ISRIL.l('Alright4');
ISRILxul.bip('Alright').textContent = contentStr;
}
ISRILoffline.queue = new Object();
ISRILoffline.queueItemLoaded = new Object();
ISRILoffline.queueSize = 0;
ISRILoffline.processed = 0;
ISRILoffline.skipped = 0;
ISRILoffline.timedOut = 0;
ISRILoffline.CloseOut();
for(i=1; i<=ISRILoffline.numOfThreads; i++) {
//id = ISRILoffline.getIframeId(i);
//ISRILxul.bi(id).parentNode.removeChild(ISRILxul.bi(id));
ISRILoffline.unlockThread(i);
}
}
},
CloseOut : function() {
//Why is this on settimeout?
setTimeout("ISRILoffline.bPrefs.setBoolPref('enable_automatic_image_resizing', ISRILoffline.oldbPref);", 1000);
},
Cancel : function() {
ISRILoffline.k = false;
ISRILoffline.CloseOut();
return true;
},
getIframeId : function(i) {
return "isRitL-iframe-"+i;
},
getF : function( id , ext) { return ISRILoffline.openF( ISRILoffline.iURL( id , ext ) ); },
iURL : function(id, suffix, noDot) {
if (!suffix) { suffix = 'html'; }
return (ISRILoffline.offlinePath + ISRILoffline.FD + ISRILoffline.prefix + id + (noDot?'':'.') + suffix);
},
openF : function(uri) {
var file = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(uri);
return file;
},
Serve : function(id) {
var offlineURL = 'file:///' + ISRILoffline.u(id);
if (ISRILoffline.getF(id).exists()) {
setTimeout("ISRIL.GoTo('" + offlineURL + "', '" + id + "');", 10);
} else {
setTimeout("ISRILxul.NotSavedOffline()", 10);
}
},
u : function(id, ext) {
return encodeURI( ISRILoffline.iURL( id , ext ).replace(/\\/g,'/') );
},
deleteOffline : function(id) {
file = ISRILoffline.getF( id );
if (file.exists()) {
file.remove(false);
file = ISRILoffline.openF( ISRILoffline.iURL(id, '_files', true) );
if (file.exists()) { file.remove(true); }
}
},
// -- Versioning -- //
//Version 0.99 changes the filenames of offline items, this will go through any items saved and rename them.
renameFiles : function() {
try {
var dir = ISRILoffline.openF( ISRILoffline.offlinePath + ISRILoffline.FD );
var list = ISRIL.GetList();
for(var i in list) {
id = list[i].itemId;
file = ISRILoffline.openF( ISRILoffline.offlinePath + ISRILoffline.FD + ISRILoffline.prefix + ISRIL.sBookmarks.getItemGUID(id) + '.html' );
if (file.exists()) {
file.moveTo(dir, ISRILoffline.prefix + id + '.html' );
//asset directory
file = ISRILoffline.openF( ISRILoffline.offlinePath + ISRILoffline.FD + ISRILoffline.prefix + ISRIL.sBookmarks.getItemGUID(id) + '_files' );
if (file.exists()) {
file.remove(true);
}
}
}
} catch(e) { }
},
}
var ISRILoffline = new ISRILoffline();